//
//  ChatVieViewController.m
//  WebRTCDemo_iOS
//
//  Created by szblsx2 on 2020/2/29.
//  Copyright © 2020 szblsx2. All rights reserved.
//

#import "ChatVieViewController.h"
#import "KLSdk.h"
#import "WHToast.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
#define KVideoWidth KScreenWidth / 2.0
#define kVideoHeight KVideoWidth

@interface ChatVieViewController ()

@property (weak, nonatomic) IBOutlet UILabel *roomNum;

@end

@implementation ChatVieViewController


#pragma mark - life cycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    

    self.roomNum.text = [NSString stringWithFormat:@"  房间：%@",self.roomId];
    
    UICollectionView *videoRenderer = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-70) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    videoRenderer.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:videoRenderer];
    [self.view sendSubviewToBack:videoRenderer];
    if (@available(iOS 11.0, *)) {
        videoRenderer.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [[KLSdk shared] setupVideoRenderer:videoRenderer];
//    __weak __typeof(self) ws = self;
    [[KLSdk shared] onEventHandleCallBack:^(NSDictionary * _Nonnull response) {
        NSInteger event = [response[@"event"] integerValue];
        NSInteger code = [response[@"code"] integerValue];
        NSString *mid = response[@"mid"];
        NSString *roomId = response[@"roomId"];
        NSLog(@"事件码：%zd，状态码：%zd，流id：%@，房间id：%@",event,code,mid,roomId);
        switch (event) {
            case SOCKET_EVENT: // socket连接事件
            {
                if (code == CODE_FAIL) {
                    // socket断开 断线重连
                }
            }
                break;
            case PEER_EVENT: // peer连接事件
            {
                if (code == CODE_FAIL) {
                    // peer断开 断线重连
                }
            }
                break;
            case MEET_CREATE: // 创建会议
            {
                [WHToast showMessage:@"创建会议成功"];
            }
                break;
            case MEET_JOIN: // 加入会议
            {
                
            }
                break;
            case MEET_LEAVE: // 离开会议
            {
                
            }
                break;
            case MEET_PEER_JOIN: // 有人加入
            {
                
            }
                break;
            case MEET_PEER_LEAVE: // 有人离开
            {
                
            }
                break;
            default:
                break;
        }
        
    }];
    
}


#pragma mark - selector
// 离开房间
- (IBAction)clickBtnForChat:(UIButton *)sender
{
    [[KLSdk shared] leaveMeeting];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//开关摄像头
- (IBAction)openCamera:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    [[KLSdk shared] turnOnCamera:!sender.isSelected];
}

//切换摄像头
- (IBAction)switchCamera:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    [[KLSdk shared] switchCamera];
}

//开关麦克风
- (IBAction)openMicrophone:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    [[KLSdk shared] turnOnMicrophone:!sender.isSelected];
}

// 开关扬声器
- (IBAction)openVoice:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    [[KLSdk shared] whetherToOpenLoudspeaker:!sender.isSelected];
}

// 测试断线(断开信令)
- (IBAction)test1:(UIButton *)sender
{
    [[KLSdk shared] test1];
}


@end
